package v1

import (
	"gin-example/models"
	"gin-example/pkg/e"
	"gin-example/pkg/setting"
	"gin-example/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"log"
	"net/http"
)

// GetArticle 获取单个
func GetArticle(c *gin.Context) {
   id := com.StrTo(c.Param("id")).MustInt()

   data := make(map[string]interface{})
   valid := validation.Validation{}
   valid.Min(id,1,"id").Message("ID必须大于0")

   code := e.INVALID_PARAMS
   if ! valid.HasErrors() {
   	  if models.ExistArticleByID(id) {
   	  	data["lists"] = models.GetArticle(id)
   	  	code = e.SUCCESS
	  } else {
	  	code = e.ERROR_NOT_EXIST_ARTICLE
	  }
   } else {
     for _, err := range valid.Errors {
     	log.Printf("err.key %s, err.message %s", err.Key, err.Message)
	 }
   }

   c.JSON(http.StatusOK, gin.H{
   	   "code": code,
   	   "msg": e.GetMsg(code),
   	   "data": data,
   })
}

// GetArticles 获取多个
func GetArticles(c *gin.Context) {
    data := make(map[string]interface{})
    maps := make(map[string]interface{})
    valid := validation.Validation{}

    var state int = -1
    if arg := c.Query("state"); arg != "" {
    	state = com.StrTo(arg).MustInt()
    	maps["state"] = state

    	valid.Range(state,0,1,"state").Message("状态只允许0或1")
	}

	var tagId int = -1
	if arg := c.Query("tag_id"); arg != "" {
		tagId = com.StrTo(arg).MustInt()
		maps["tag_id"] = tagId

		valid.Min(tagId,1,"tag_id").Message("标签ID必须大于0")
	}

	code := e.INVALID_PARAMS
	if ! valid.HasErrors() {
		data["lists"] = models.GetArticles(util.GetPage(c), setting.PageSize, maps)
		data["total"] = models.GetArticleTotal(maps)
		code = e.SUCCESS
	} else {
		for _ , err := range valid.Errors {
			log.Printf("err.Key %s, err.Message %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": data,
	})
}

// AddArticle 添加
func AddArticle(c *gin.Context) {
    tagId := com.StrTo(c.Query("tag_id")).MustInt()
    title := c.Query("title")
    desc := c.Query("desc")
    content := c.Query("content")
    createdBy := c.Query("created_by")
    state := com.StrTo(c.DefaultQuery("state","0")).MustInt()

    valid := validation.Validation{}
    valid.Min(tagId,1,"tag_id").Message("标签ID必须大于0")
    valid.Required(title, "title").Message("标题不能为空")
    valid.Required(desc,"desc").Message("描述不能为空")
    valid.Required(content,"content").Message("内容不能为空")
    valid.Required(createdBy,"created_by").Message("创建者不能为空")
    valid.Range(state,0,1,"state").Message("状态只允许0或1")

    code := e.INVALID_PARAMS
    if ! valid.HasErrors() {
    	if models.ExistTagByID(tagId) {
           data := make(map[string]interface{})
           data["tag_id"] = tagId
           data["title"] = title
           data["desc"] = desc
           data["content"] = content
           data["created_by"] = createdBy
           data["state"] = state

           models.AddArticle(data)
           code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_TAG
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.Key %s, err.Message %s", err.Key,err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]interface{}),
	})
}

// EditArticle 更新
func EditArticle(c *gin.Context) {
    valid := validation.Validation{}

    id := com.StrTo(c.Query("id")).MustInt()
    tagId := com.StrTo(c.Query("tag_id")).MustInt()
    title := c.Query("title")
    desc := c.Query("desc")
    content := c.Query("content")
    modifiedBy := c.Query("modified_by")

    var state int = -1
    if arg := c.Query("state"); arg != "" {
    	state = com.StrTo(arg).MustInt()
    	valid.Range(state,0,1,"state").Message("状态只允许0或1")
	}

	valid.Min(id,1,"id").Message("id必须大于0")
    valid.MaxSize(title,100,"title").Message("标题最长为100字符")
    valid.MaxSize(desc,255,"desc").Message("简述最长为255字符")
    valid.MaxSize(content,65535,"content").Message("内容最长为65535")
    valid.Required(modifiedBy,"midified_by").Message("修改人不能为空")
    valid.MaxSize(modifiedBy,100,"modified_by").Message("修改人最长为100字符")

    code := e.INVALID_PARAMS
    if ! valid.HasErrors() {
    	if models.ExistArticleByID(id) {
    		if models.ExistTagByID(tagId) {
    			data := make(map[string]interface{})
    			if tagId > 0 {
    				data["tag_id"] = tagId
				}

				if title != "" {
					data["title"] = title
				}

				if desc != "" {
					data["desc"] = desc
				}

				if content != "" {
					data["content"] = content
				}
				data["modified_by"] = modifiedBy

				models.EditTag(id, data)
				code = e.SUCCESS
			} else {
				code = e.ERROR_NOT_EXIST_TAG
			}
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
	} else {
		for _, err := range valid.Errors {
			log.Printf("err.Key: %s, err.Message: %s", err.Key, err.Message)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg": e.GetMsg(code),
		"data": make(map[string]string),
	})

}

// DeleteArticle 删除
func DeleteArticle(c *gin.Context) {
   id := com.StrTo(c.Param("id")).MustInt()

   valid := validation.Validation{}
   valid.Min(id,1,"id").Message("ID必须大于0")

   code := e.INVALID_PARAMS
   if ! valid.HasErrors() {
   	    if models.ExistArticleByID(id) {
   	    	models.DeleteArticle(id)
   	    	code = e.SUCCESS
		} else {
			code = e.ERROR_NOT_EXIST_ARTICLE
		}
   } else {
   	  for _, err := range valid.Errors {
   	  	log.Printf("err.Key %s, err.Message %s", err.Key,err.Message)
	  }
   }

   c.JSON(http.StatusOK, gin.H{
   	  "code": code,
   	  "msg": e.GetMsg(code),
   	  "data": make(map[string]interface{}),
   })
}
